package cn.crystalling.app.attendance;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Scanner;

@SuppressWarnings("all")
public class DataHelper {

    public static ArrayList<Student> readCSV(Context context, String path) {
        ArrayList<Student> stuArr = new ArrayList<>();
        File file = new File(path);
        FileInputStream fileInputStream;
        Scanner in;
        try {
            fileInputStream = new FileInputStream(file);
            //一定要声明为GBK编码，因为CSV文件在windows平台编辑的
            in = new Scanner(fileInputStream, "GBK");
            in.nextLine();
            while (in.hasNextLine()) {
                String[] lines = in.nextLine().split(",");
                Student stu = new Student(Integer.parseInt(lines[0]), lines[1]/*, lines[2], lines[3], Integer.parseInt(lines[4])*/);
                Log.e("readCSV", lines[0] + "    " + lines[1]);
                stuArr.add(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stuArr;
    }

    public static boolean copyCSV(Context context, String srcFileName, String destFileName) {
        String MESSAGE;
        File srcFile = new File(srcFileName);

        // 判断源文件是否存在
        if (!srcFile.exists()) {
            MESSAGE = "源文件：" + srcFileName + "不存在！";
            Toast.makeText(context, MESSAGE, Toast.LENGTH_SHORT).show();
            return false;
        } else if (!srcFile.isFile()) {
            MESSAGE = "复制文件失败，源文件：" + srcFileName + "不是一个文件！";
            Toast.makeText(context, MESSAGE, Toast.LENGTH_SHORT).show();
            return false;
        }

        // 判断目标文件是否存在
        File destFile = new File(destFileName);
        // 如果目标文件所在目录不存在，则创建目录
        if (!destFile.getParentFile().exists()) {
            // 目标文件所在目录不存在
            if (!destFile.getParentFile().mkdirs()) {
                // 复制文件失败：创建目标文件所在目录失败
                return false;
            }
        }

        // 复制文件
        int byteread = 0; // 读取的字节数
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(destFile);
            byte[] buffer = new byte[1024];

            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread);
            }
            return true;
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

